import { FilterColumn, Query } from './query'

/**
 * 路由参数
 */
export interface RouterParams extends PagingSort {
  [key: string]: any
}
/**
 * 排序方式
 */
export type SortWay = 'asc' | 'desc'

/**
 * 分页接口
 */
export interface PagingSort {
  currentPage?: number
  endIndex?: number
  pageCount?: number
  pageSize?: number
  startIndex?: number
  totalCount?: number
  sortField?: string
  sortFieldType?: string
  sortWay?: SortWay
}

/**
 * 排序接口
 */
export interface Sort {
  sortField?: string
  sortWay?: SortWay
}

/**
 * 查询条件接口
 */
export interface QueryParameter {
  columns: FilterColumn[]
  sortList: Sort[]
  pagingSort: PagingSort
}

export interface QueryParameterOptions {
  defaultSortField?: string
  defaultSortWay?: SortWay
  sortList?: Sort[]
}

/**
 * 查询工具类
 */
export class QueryUtils {
  /**
   * 创建Query类
   * @returns
   */
  static builder(filterValues?: any[]): Query {
    return new Query(filterValues)
  }

  /**
   * 构建查询参数
   */
  static queryParameter(
    pagingSort: PagingSort,
    columns: FilterColumn[] | Query = [],
    queryParameterOptions: QueryParameterOptions = { defaultSortWay: 'asc', defaultSortField: '' }
  ): QueryParameter {
    const { pageSize = 50, currentPage = 1, sortField, sortWay } = pagingSort
    const {
      defaultSortField,
      defaultSortWay,
      sortList: defaultSortList = [],
    } = queryParameterOptions
    const pagingSortInfo: PagingSort = {
      pageSize: parseInt(pageSize as any),
      currentPage: parseInt(currentPage as any),
    }
    const sortList: Sort[] = []
    if (sortField && sortWay) {
      sortList.push({
        sortField,
        sortWay,
      })
      pagingSortInfo.sortField = sortField
      pagingSortInfo.sortWay = sortWay
    } else if (defaultSortField && defaultSortWay) {
      sortList.push({
        sortField: defaultSortField,
        sortWay: defaultSortWay,
      })
      pagingSortInfo.sortField = defaultSortField
      pagingSortInfo.sortWay = defaultSortWay
    }
    if (defaultSortList.length > 0) {
      sortList.push(...defaultSortList)
    }
    return {
      columns: QueryUtils.getFilterColumns(columns),
      pagingSort: pagingSortInfo,
      sortList,
    }
  }

  /**
   * 添加默认排序
   * @param queryParameter
   * @param queryParameterOptions
   * @returns
   */
  static addDefaultSortInfo(
    queryParameter: QueryParameter,
    queryParameterOptions: QueryParameterOptions = { defaultSortWay: 'asc', defaultSortField: '' }
  ) {
    const { defaultSortField, defaultSortWay, sortList = [] } = queryParameterOptions
    if (queryParameter.sortList.length === 0 && defaultSortField && defaultSortWay) {
      queryParameter.pagingSort.sortField = defaultSortField
      queryParameter.pagingSort.sortWay = defaultSortWay
      queryParameter.sortList.push({
        sortField: defaultSortField,
        sortWay: defaultSortWay,
      })
    }
    if (sortList.length > 0) {
      queryParameter.sortList.push(...sortList)
    }
    return queryParameter
  }

  /**
   * 转换响应体
   * @param dataSource
   * @returns
   */
  static convertResponse<T = { [key: string]: any }>(dataSource): PagingSort & { data: T[] } {
    const pagingSort = dataSource?.queryParameter?.pagingSort || dataSource.pageInfo || {}
    const data = dataSource?.data || dataSource?.resultSet || []
    return {
      ...pagingSort,
      data,
    }
  }

  static defaultParameter(params: RouterParams = {}): RouterParams {
    return {
      totalCount: 0,
      currentPage: 1,
      pageSize: 50,
      sortField: '',
      sortWay: 'asc',
      ...params,
    }
  }

  /**
   * 获取查询条件
   * @param query
   * @returns
   */
  static getFilterColumns(query: Query | FilterColumn[]) {
    return query instanceof Query ? query.getFilterColumns() : query
  }
}
